package itsm.isperp.module.service.incident;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.DataRequest;
import itsm.isperp.framework.service.BaseService;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.incident.IcdCategoryProperty;
import itsm.isperp.module.repository.incident.IcdCategoryPropertyMapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * 服务
 * 
 * @author lizx
 * @date 2016-07-21 18:05
 */
@Service
public class IcdCategoryPropertyService extends
		BaseService<IcdCategoryProperty> {

	@Autowired
	protected IcdCategoryPropertyMapper icdCategoryPropertyMapper;

	@Override
	public IcdCategoryPropertyMapper getRepository() {
		return this.icdCategoryPropertyMapper;
	}

	/**
	 * 查询指定分类的属性（针对当前分类的属性）
	 * 
	 * @return
	 */
	public List<IcdCategoryProperty> findProperty(String categoryId) {
		DataRequest req = new JqGridRequest();
		req.isPageable(false);
		req.addFilterRule("confCategoryId", JqGridFilterRuleOp.eq, categoryId); // 编号
		return icdCategoryPropertyMapper.findAllByRequest(req).getContent();
	}

	/**
	 * 查询指定分类的属性（针对当前分类以及其父级分类的属性）
	 * 
	 * @return
	 */
	public List<Map<String, Object>> findAllProperty(String categoryId) {

		if (StringUtils.isEmpty(categoryId)) {
			String sql = "SELECT '' as id,a.name FROM icd_category_property a LEFT JOIN icd_incident_property b ON a.id=b.category_property_id WHERE EXISTS(SELECT 1 FROM icd_incident WHERE id=b.relation_id) GROUP BY a.name";
			JdbcTemplate jdbcTemplate = ContextHolder
					.getSpringBean("jdbcTemplate");
			return jdbcTemplate.queryForList(sql);
		} else {
			return icdCategoryPropertyMapper
					.findAllPropertyByCategoryId(categoryId);
		}

	}
	
	public Map<String, Object> getCategoryInfo(String categoryId) {

		if (!StringUtils.isEmpty(categoryId)) {
			String sql = "select max(order_num)+1 from icd_category_property where category_id='"+categoryId+"'";
			JdbcTemplate jdbcTemplate = ContextHolder
					.getSpringBean("jdbcTemplate");
			String sql2="select classify from icd_category_property where category_id='"+categoryId+"' group by classify";
			List<Map<String,Object>> list=jdbcTemplate.queryForList(sql2);
			Integer maxNum=jdbcTemplate.queryForObject(sql, Integer.class);
			Map<String,Object> map=new HashMap<String,Object>();
			map.put("maxNum", maxNum);
			map.put("classifyList", list);
			return map;
			
		} else {
			return null;
		}

	}

	public List<IcdCategoryProperty> findAllPropertyEx(String categoryId) {

		return icdCategoryPropertyMapper
				.findAllPropertyByCategoryIdEx(categoryId);
	}

	public int getPropertyCountByCategoryPath(String categoryPath) {
		if (StringUtils.isEmpty(categoryPath)) {
			return 0;
		}

		JqGridRequest req = new JqGridRequest();
		req.isPageable(false);
		req.addSelectColumns("COUNT(1) AS count_t");
		req.addFilterRule("treenode_path", JqGridFilterRuleOp.bw, categoryPath);

		return Integer.parseInt(this.getRepository()
				.findAllDetailByRequest(req).getContent().get(0).get("count_t")
				.toString());
	}

	/**
	 * 根据配置项分类删除其其自定义属性（包括其对应的资产的自定义属性值）
	 * 
	 * @param categoryId
	 *            配置项分类ID
	 * @return void
	 */
	public void deleteCategoryPropertyByCategoryPath(String categoryPath) {
		String sql = "DELETE FROM icd_category_property WHERE  category_id  IN (SELECT id FROM icd_category WHERE treenode_path LIKE  '"
				+ categoryPath + "%' )";
		JdbcTemplate jdbcTemplate = ContextHolder.getSpringBean("jdbcTemplate");
		jdbcTemplate.update(sql);
	}
}
